package eu.irreality.dai.util;

import java.util.Iterator;

import eu.irreality.dai.ui.cell.DisplayPriorityComparator;
import eu.irreality.dai.ui.cell.DisplayableObject;

public final class GridUtilities
{

    private GridUtilities()
    {

    }

    @SuppressWarnings("unchecked")
    public static SortedList[][] simpleToComplexGrid(DisplayableObject[][] grid)
    {
	SortedList[][] newGrid = new SortedList[grid.length][grid[0].length];
	if (grid.length == 0)
	    return newGrid;
	for (int i = 0; i < grid.length; i++)
	{
	    for (int j = 0; j < grid[0].length; j++)
	    {
		newGrid[i][j] = new SortedList(new DisplayPriorityComparator());
		newGrid[i][j].add(grid[i][j]);
	    }
	}
	return newGrid;
    }

    /**
     * Returns a String representation of a given SortedList grid.
     * 
     * @param grid
     *            SortedList grid to be represented.
     * @return String representation of the SortedList grid.
     */
    public static String gridListToString(SortedList[][] grid)
    {
	StringBuilder builder = new StringBuilder();
	for (int i = 0; i < grid.length; i++)
	{
	    for (int j = 0; j < grid[0].length; j++)
	    {
		SortedList dispObjs = grid[i][j];
		for (Iterator<DisplayableObject> iter = dispObjs.iterator(); iter
			.hasNext();)
		{
		    DisplayableObject dispObj = iter.next();
		    builder.append(dispObj.getChar());
		}
	    }
	    builder.append('\n');
	}
	return builder.toString();
    }

    /**
     * Returns a String representation of a given grid.
     * 
     * @param grid
     *            grid to be represented.
     * @return String representation of the grid.
     */
    public static String gridToString(DisplayableObject[][] grid)
    {
	StringBuilder builder = new StringBuilder();
	for (int i = 0; i < grid.length; i++)
	{
	    for (int j = 0; j < grid[0].length; j++)
	    {
		builder.append(grid[i][j].getChar());
	    }
	    builder.append('\n');
	}
	return builder.toString();
    }

}
